/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Characterreferences;
import com.cashmate.hibernate.pojo.Dependents;
import com.cashmate.hibernate.pojo.LoanappRequirement;
import com.cashmate.hibernate.pojo.Loanapplications;
import com.cashmate.hibernate.pojo.Loanswithotherbanks;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class LoanapplicationDAO extends DAO{

    public LoanapplicationDAO() {
        super();
    }
    
    //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Loanapplications> getAll() throws Exception {
        List<Loanapplications> a = (List<Loanapplications>) getSession().createQuery("from Loanapplications a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Loanapplications get(int id) throws Exception {
        Loanapplications a = (Loanapplications) getSession().createQuery("from Loanapplications a where a.loanappid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Loanapplications a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Loanapplications a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Loanapplications> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Loanapplications> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    //OTHER METHODS
    @SuppressWarnings("unchecked")
    public Loanapplications getLatestloan(int borid) throws Exception {
        Loanapplications la = (Loanapplications) getSession().createQuery("from Loanapplications l where l.borrowers.borrowerid=:bid order by l.loanappid desc").setParameter("bid", borid).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return la;
    }

// USE get(int id) above
//    @SuppressWarnings("unchecked")
//    public Loanapplications getById(int laid){
//        Loanapplications l=null;
//        try{
//            l = (Loanapplications) session.createQuery("from Loanapplications l where l.loanappid=:laid")
//                    .setParameter("laid", laid)
//                    .setMaxResults(1)
//                    .uniqueResult();
//        }catch(Exception ex){
//            ex.printStackTrace();
//        }
//        return l;
//    }
    @SuppressWarnings("unchecked")
    public Loanapplications getByLoanVerId(int lvid) throws Exception {
        Loanapplications l = (Loanapplications) getSession().createQuery("from Loanapplications l where l.loanverifications.loanverificationid=:lvid").setParameter("lvid", lvid).setCacheable(true).setMaxResults(1).uniqueResult();
        flush();
        return l;
    }

    @SuppressWarnings("unchecked")
    public boolean preapproveLoanapp(Loanapplications s) throws Exception {
        int loanappid = s.getLoanappid();
        try {
            begin();
            getSession().createQuery("update Loanapplications set status=:stat where loanappid=:laid").setParameter("laid", loanappid).setParameter("stat", "preapproved").executeUpdate();
            commit();
            flush();
            return true;
        } catch (Exception ex) {
            rollback();
            throw ex;
        }

    }

    @SuppressWarnings("unchecked")
    public List<Loanapplications> getApplicationsByStatus(String status, int branch) throws Exception {
        List<Loanapplications> loanapplist = getSession().createQuery("from Loanapplications l where l.status = :status and l.branches.branchid = :branch").setParameter("status", status).setCacheable(true).setParameter("branch", branch).list();
        flush();
        return loanapplist;
    }

    @SuppressWarnings("unchecked")
    public List<Loanapplications> getApplicationsByStatus(String status) throws Exception {
        List<Loanapplications> loanapplist = getSession().createQuery("from Loanapplications l where l.status = :status").setParameter("status", status).setCacheable(true).list();
        flush();
        return loanapplist;
    }

    @SuppressWarnings("unchecked")
    public List<Loanapplications> getApplicationsByRequest(int id) throws Exception {
        List<Loanapplications> loanapplist = getSession().createQuery("from Loanapplications l where l.request.requestid = :id").setParameter("id", id).setCacheable(true).list();
        flush();
        return loanapplist;
    }

    @SuppressWarnings("unchecked")
    public List<Dependents> getDependentsByLoanapp(int id) throws Exception {
        //System.out.println("NAPUNTA SA DEPENDENTS");
        List<Dependents> loanapplist = (List<Dependents>) getSession().createQuery("from Dependents d where d.loanapplications.loanappid = :lid").setParameter("lid", id).setCacheable(true).list();
        //System.out.println("dependents " + loanapplist.get(1).getName());
        flush();
        return loanapplist;
    }

    @SuppressWarnings("unchecked")
    public List<LoanappRequirement> getRequirementsByLoanapp(int id) throws Exception {
        List<LoanappRequirement> loanapplist = (List<LoanappRequirement>) getSession().createQuery("from LoanappRequirement r where r.loanapplications.loanappid = :lid").setParameter("lid", id).setCacheable(true).list();
        //System.out.println("loanappreq " + loanapplist.get(1).getLoanappRequirementid());
        flush();
        return loanapplist;
    }

    @SuppressWarnings("unchecked")
    public List<Characterreferences> getReferencesByLoanapp(int id) throws Exception {
        List<Characterreferences> loanapplist = (List<Characterreferences>) getSession().createQuery("from Characterreferences r where r.loanapplications.loanappid = :lid").setParameter("lid", id).setCacheable(true).list();
        //System.out.println("character " + loanapplist.get(1).getName());
        flush();
        return loanapplist;
    }

    @SuppressWarnings("unchecked")
    public List<Loanswithotherbanks> getLoanswithotherByLoanapp(int id) throws Exception {
        List<Loanswithotherbanks> loanapplist = (List<Loanswithotherbanks>) getSession().createQuery("from Loanswithotherbanks r where r.loanapplications.loanappid = :lid").setParameter("lid", id).setCacheable(true).list();
        //System.out.println("dependents " + loanapplist.get(1).getBankname());
        flush();
        return loanapplist;
    }
}
